package category

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"go.uber.org/zap"
	"google.golang.org/protobuf/types/known/emptypb"
	"mxshop-api/goods-web/api"
	"mxshop-api/goods-web/forms"
	"mxshop-api/goods-web/global"
	"mxshop-api/goods-web/proto"
	"net/http"
	"strconv"
)

/*
*
获取分类列表
*/
func List(ctx *gin.Context) {
	list, err := global.GoodsSrvClient.GetAllCategorysList(ctx, &emptypb.Empty{})
	if err != nil {
		api.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	result := make([]interface{}, 0)
	data := list.JsonData
	err = json.Unmarshal([]byte(data), &result)
	if err != nil {
		zap.S().Errorw("[List]查询分类列表失败", err.Error())
	}
	ctx.JSON(http.StatusOK, result)
}

/*
*
获取分类详情
*/
func Detail(ctx *gin.Context) {
	id := ctx.Param("id")
	i, err := strconv.ParseInt(id, 10, 32)
	if err != nil {
		ctx.Status(http.StatusNotFound)
	}
	data, err := global.GoodsSrvClient.GetSubCategory(ctx, &proto.CategoryListRequest{
		Id: int32(i),
	})
	if err != nil {
		api.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	info := data.Info
	categorys := data.SubCategorys
	resultMap := make(map[string]interface{}, 0)
	subCategorys := make([]interface{}, 0)
	for _, subCategory := range categorys {
		subCategorys = append(subCategorys, map[string]interface{}{
			"id":              subCategory.Id,
			"name":            subCategory.Name,
			"level":           subCategory.Level,
			"parent_category": subCategory.ParentCategory,
			"is_tab":          subCategory.IsTab,
		})
	}
	resultMap["id"] = info.Id
	resultMap["name"] = info.Name
	resultMap["level"] = info.Level
	resultMap["parent_category"] = info.ParentCategory
	resultMap["is_tab"] = info.IsTab
	resultMap["sub_categors"] = subCategorys
	ctx.JSON(http.StatusOK, resultMap)
}

/*
*
创建分类
*/
func New(ctx *gin.Context) {
	categoryForm := forms.CategoryForm{}
	err := ctx.ShouldBindJSON(&categoryForm)
	if err != nil {
		api.HandleValidatorError(ctx, err)
		return
	}
	category, err := global.GoodsSrvClient.CreateCategory(ctx, &proto.CategoryInfoRequest{
		Name:           categoryForm.Name,
		ParentCategory: categoryForm.ParentCategory,
		Level:          categoryForm.Level,
		IsTab:          *categoryForm.IsTab,
	})
	if err != nil {
		api.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	resultMap := make(map[string]interface{}, 0)
	resultMap["id"] = category.Id
	resultMap["name"] = category.Name
	resultMap["parent_category"] = category.ParentCategory
	resultMap["level"] = category.Level
	resultMap["is_tab"] = category.IsTab
	ctx.JSON(http.StatusOK, resultMap)
}

/*
*
修改分类
*/
func Update(ctx *gin.Context) {
	//校验表单
	updateCategoryForm := forms.UpdateCategoryForm{}
	err := ctx.ShouldBindJSON(&updateCategoryForm)
	if err != nil {
		api.HandleValidatorError(ctx, err)
		return
	}
	//获取id
	id := ctx.Param("id")
	i, err := strconv.ParseInt(id, 10, 32)
	if err != nil {
		ctx.Status(http.StatusNotFound)
	}
	//进行修改
	request := proto.CategoryInfoRequest{
		Id:   int32(i),
		Name: updateCategoryForm.Name,
	}
	if updateCategoryForm.IsTab != nil {
		request.IsTab = *updateCategoryForm.IsTab
	}
	_, err = global.GoodsSrvClient.UpdateCategory(ctx, &request)
	//处理返回值
	if err != nil {
		api.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	ctx.Status(http.StatusOK)
}

/*
*
删除分类
*/
func Delete(ctx *gin.Context) {
	id := ctx.Param("id")
	i, err := strconv.ParseInt(id, 10, 32)
	if err != nil {
		ctx.Status(http.StatusNotFound)
	}
	_, err = global.GoodsSrvClient.DeleteCategory(ctx, &proto.DeleteCategoryRequest{
		Id: int32(i),
	})
	if err != nil {
		api.HandleGrpcErrorToHttp(err, ctx)
		return
	}
	ctx.Status(http.StatusOK)
}
